import {compare, isInArray} from '@/utils'
import { MissonModel } from '../room/Misson/missonModel'
export default class LabExtension extends StructureLab {

     /* 任务系统3.0 lab处理任务逻辑 */
    public HandMisson():void{
        if (!this.room.memory.Task['STRUCTURE']) this.room.memory.Task['STRUCTURE'] = []
        var allmyTask = []
        for (var task of this.room.memory.Task['STRUCTURE'])
        {
            if (isInArray(task.relateStructure,this.id))
            {
                allmyTask.push(task)
            }
        }
        /* 按照优先级排序 */
        if (allmyTask.length <= 0) return
        else if (allmyTask.length > 1)
            allmyTask.sort(compare('taskLevel'))
        /* 处理任务 */
        let thisTask = allmyTask[0]
        if (thisTask.delayTick < 99995)
            thisTask.delayTick--
        switch (thisTask.Name){
            case "lab合成":{this.HandleLC();break}
        }
    }

    /* 处理合成任务 */
    HandleLC():void{
        /* 找到任务 */
        if (this.cooldown) return
        var myTask:RoomTask
        LoopLink:
        for (var i of this.room.memory.Task['STRUCTURE'])
        {
            if (isInArray(i.relateStructure,this.id))
            {
                myTask = i
                break LoopLink
            }
        }
        /* 确定任务建筑 */
        var raw1Lab = Game.getObjectById(myTask.disStructure[0]) as StructureLab
        var raw2Lab = Game.getObjectById(myTask.disStructure[1]) as StructureLab
        if (!raw1Lab || !raw2Lab || raw1Lab.IdentifyImpurity(myTask.disResource[0]) || raw2Lab.IdentifyImpurity(myTask.disResource[1]))  {this.room.deleteMisson_ID(myTask.ID);console.log("[lab]目标建筑存在杂质，任务终止!");return}
        var disStructure =[{lab:raw1Lab,rtype:myTask.disResource[0]},{lab:raw2Lab,rtype:myTask.disResource[1]}]
        for (var l of disStructure)
        {
            if (l.lab.store[l.rtype] < 1500 && !this.room.getMisson_S('CREEP','物流搬运','transport01'))
            {
                /* 下达搬运任务 */
                var TaskCreep:RoomTask = {} as RoomTask
                Object.assign(TaskCreep,MissonModel['物流搬运'])
                TaskCreep.relateCreep = ['transport01']
                TaskCreep.disnum = 2500
                TaskCreep.relateResource = [l.rtype]
                TaskCreep.disStructure = [l.lab.id]
                TaskCreep.taskLevel = 10
                TaskCreep.sourceStructure = [this.room.memory.TaskMemory.storageID as string]
                this.room.mountMisson(TaskCreep)
            }
        }
        this.runReaction(raw1Lab,raw2Lab)
        if (this.store[myTask.relateResource[0]] > 2500 && !this.room.getMisson_S('CREEP','物流搬运','transport01'))
        {
            /* 如果该lab是多任务的，就不搬走 */
            if (this.room.memory.TaskData.OccupiedLab[this.id].num > 2) return
                var TaskCreep:RoomTask = {} as RoomTask
                Object.assign(TaskCreep,MissonModel['物流搬运'])
                TaskCreep.relateCreep = ['transport01']
                TaskCreep.sourcenum = 0
                TaskCreep.relateResource = [myTask.relateResource[0]]
                TaskCreep.disStructure = [this.room.memory.TaskMemory.storageID as string]
                TaskCreep.taskLevel = 1
                TaskCreep.sourceStructure = [this.id]
                this.room.mountMisson(TaskCreep)
        }
    }

        /* 判断lab中是否有多余杂质的函数 */
        public IdentifyImpurity(res:ResourceConstant):boolean
        {
            for (var i in this.store)
            {
                if (i != res && i != 'energy')
                return true
            }
            return false
        }
}